package com.psbc.redis_study.controller;

import com.psbc.redis_study.dao.VideoDao;
import com.psbc.redis_study.model.VideoDo;
import com.psbc.redis_study.utils.JsonData;
import com.psbc.redis_study.utils.JsonUtil;
import com.psbc.redis_study.vo.CartItemVo;
import com.psbc.redis_study.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v1/cart")
public class CartController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private VideoDao videoDao;

    /**
     * 添加购物车
     * @param videoId
     * @param buyNum
     * @return
     */
    @RequestMapping("/add")
    public JsonData addCart(int videoId,int buyNum){
        //首先应该获得购物车
        BoundHashOperations<String, Object, Object> myCart = getMyCart();
        //获得购物车里的商品
        Object objCache = myCart.get(videoId + "");
        String result = "";
        if (objCache != null){
            result = (String) objCache;
        }
        //购物车没有这个商品
        if (objCache == null){
            CartItemVo cartItem = new CartItemVo();
            VideoDo videoDo = videoDao.findDetailById(videoId);
            cartItem.setBuyNum(buyNum);
            cartItem.setPrice(videoDo.getPrice());
            cartItem.setProductId(videoId);
            cartItem.setProductImg(videoDo.getMsg());
            cartItem.setProductTitle(videoDo.getTitle());
            myCart.put(videoId+"", JsonUtil.objectToJson(cartItem));
        }else {
            //购物车已经有了这个商品，就要增加商品数量
            CartItemVo cartItemVo = JsonUtil.jsonToPojo(result, CartItemVo.class);
            cartItemVo.setBuyNum(cartItemVo.getBuyNum()+buyNum);
            myCart.put(videoId+"", JsonUtil.objectToJson(cartItemVo));
        }
        return JsonData.buildSuccess();
    }

    /**
     * 获得我的购物车的通用方法
     * @return
     */
    private BoundHashOperations<String,Object,Object> getMyCart(){
        String key = getKey();
        BoundHashOperations boundHashOperations = redisTemplate.boundHashOps(key);
        return boundHashOperations;
    }

    /**
     * 得到购物车的key
     * @return
     */
    private String getKey(){
        //其实这个id正常来说应该从拦截器中获取
        int userID = 88;
        String cartKey = String.format("video:cart:%s",userID);
        return cartKey;
    }

    /**
     * 查看我的购物车
     */
    @RequestMapping("/see")
    public JsonData seeMyCart(){
        BoundHashOperations<String, Object, Object> myCart = getMyCart();
        List<Object> itemsList = myCart.values();
        ArrayList<CartItemVo> cartItemVos = new ArrayList<>();
        for (Object item : itemsList) {
            CartItemVo cartItemVo = JsonUtil.jsonToPojo((String)item, CartItemVo.class);
            cartItemVos.add(cartItemVo);
        }
        //封装成cartVo
        CartVo cartVo = new CartVo();
        cartVo.setCartItems(cartItemVos);
        return JsonData.buildSuccess(cartVo);
    }
    /**
     * 清空我的购物车
     */
    @RequestMapping("/clear")
    public JsonData clearMyCart(){
        String key = getKey();
        redisTemplate.delete(key);
        return JsonData.buildSuccess();
    }
}
